﻿Imports Global_Visas_Business_Layer
Imports System.Data
Imports System.Reflection
Imports System.Windows.Controls
Imports System.Text
Imports System.ComponentModel

Partial Public Class frmAdminCustomizeCRMView
    Public g_MenuItem1 As Integer
    Public g_MenuItem2 As Integer
    Public g_MenuItem3 As Integer
    Public g_MenuItem4 As Integer
    Public g_MenuItem5 As Integer
    Public g_MenuItem6 As Integer
    Public g_MenuItem7 As Integer
    Public g_MenuItem8 As Integer
    '    Public g_MenuItem8 As Integer

    Private Sub frmAdminCustomizeCRMView_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        Dim dt As DataTable
        Try
            GeneralTasks.ChangeTheme(Me)
            GeneralTasks.EnumerateandFill(Me.LogicalChildren)
            LoadListMenuItems()
            LoadListColumnItems()
            dt = Admin.GetCurrentMenuLayout(GlobalCRM.CurrentConID)
            If Not dt Is Nothing Then
                If dt.Rows.Count > 0 Then
                    txtLayout1.Text = dt.Rows(0).Item("LO1")
                    txtLayout2.Text = dt.Rows(0).Item("LO2")
                    txtLayout3.Text = dt.Rows(0).Item("LO3")
                End If
            End If
        Catch ex As Exception

        Finally
            dt = Nothing
        End Try
    End Sub
    Private Sub LoadListMenuItems()
        Dim bind As New Binding()
        Dim dt As DataTable
        Try
            dt = Admin.MenuLayoutForConsultant(GlobalCRM.CurrentConID)
            bind.Source = dt
            lstConsView.SetBinding(ListView.ItemsSourceProperty, bind)
        Catch ex As Exception

        Finally
            dt = Nothing
            bind = Nothing
        End Try
    End Sub
    Private Sub LoadListColumnItems(Optional ByVal Bookmark As Admin.MenuOptionList = Admin.MenuOptionList.All)
        Dim bind As New Binding()
        Dim dt As DataTable
        Try
            dt = Admin.GetMenuBookMarks(Bookmark)
            bind.Source = dt
            lstAvailColumns.SetBinding(ListView.ItemsSourceProperty, bind)
        Catch ex As Exception

        Finally
            dt = Nothing
            bind = Nothing
        End Try
    End Sub

    Private Sub btnLayout1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnLayout1.Click
        If lstConsView.SelectedItems.Count > 0 Then
            If Admin.SetMenuLayout(1, lstConsView.SelectedItems(0).row(0).ToString, GlobalCRM.CurrentConID) = True Then
                txtLayout1.Text = lstConsView.SelectedItems(0).row(1).ToString
            End If
        End If
    End Sub

    Private Sub btnLayout2_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnLayout2.Click
        If lstConsView.SelectedItems.Count > 0 Then
            If Admin.SetMenuLayout(2, lstConsView.SelectedItems(0).row(0).ToString, GlobalCRM.CurrentConID) = True Then
                txtLayout2.Text = lstConsView.SelectedItems(0).row(1).ToString
            End If
        End If
    End Sub

    Private Sub btnLayout3_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnLayout3.Click
        If lstConsView.SelectedItems.Count > 0 Then
            If Admin.SetMenuLayout(3, lstConsView.SelectedItems(0).row(0).ToString, GlobalCRM.CurrentConID) = True Then
                txtLayout3.Text = lstConsView.SelectedItems(0).row(1).ToString
            End If
        End If
    End Sub

    Private Sub btnEditLayout_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnEditLayout.Click

        If lstConsView.SelectedItems(0).row.itemarray(2).ToString = "System" Then
            MsgBox("The system layout cannot be edited.", MsgBoxStyle.Critical, "System Menu")
        End If
        btnSaveLayout.Content = "Save"
        'btnSaveLayout.IsEnabled = True
        grpSelect.IsEnabled = True
        btnNewLayout.IsEnabled = False
        btnEditLayout.IsEnabled = False
        btnSaveLayout.IsEnabled = True

        btnSendLayout1.IsEnabled = True
        btnSendLayout2.IsEnabled = True
        btnSendLayout3.IsEnabled = True
        btnSendLayout4.IsEnabled = True
        btnSendLayout5.IsEnabled = True
        btnSendLayout6.IsEnabled = True
        btnSendLayout7.IsEnabled = True
        btnSendLayout8.IsEnabled = True
    End Sub

    Private Sub btnNewLayout_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnNewLayout.Click
        btnSaveLayout.Content = "Save New"
        btnSaveLayout.IsEnabled = True
        grpSelect.IsEnabled = True
        txtSelectedLayout.Text = String.Empty
        txtSelectedLayout.IsReadOnly = False
        btnSendLayout1.IsEnabled = True
        btnSendLayout2.IsEnabled = True
        btnSendLayout3.IsEnabled = True
        btnSendLayout4.IsEnabled = True
        btnSendLayout5.IsEnabled = True
        btnSendLayout6.IsEnabled = True
        btnSendLayout7.IsEnabled = True
        btnSendLayout8.IsEnabled = True
        btnNewLayout.IsEnabled = False
    End Sub

    Private Sub btnSaveLayout_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnSaveLayout.Click
        Dim msg As StringBuilder
        Dim TasksEvents As New TasksEvents
        Try
            If txtSelectedLayout.Text = String.Empty Then
                MsgBox("Name cannot be empty", MsgBoxStyle.Information, "Layout Name")
                Exit Sub
            End If
            If txtColumn1.Text = String.Empty Or _
                txtColumn2.Text = String.Empty Or _
                txtColumn3.Text = String.Empty Or _
                txtColumn4.Text = String.Empty Or _
                txtColumn5.Text = String.Empty Or _
                txtColumn6.Text = String.Empty Or _
                txtColumn7.Text = String.Empty Or _
                txtColumn8.Text = String.Empty Or _
                txtColumn9.Text = String.Empty Then
                MsgBox("The Layout columns cannot be empty, please select appropriate column from the column list", MsgBoxStyle.Critical, "Layout Columns")
                Exit Sub
            End If
            Select Case Me.btnSaveLayout.Content
                Case "Save New"
                    'insert function
                    msg = New StringBuilder
                    Dim arrMenu(7) As String
                    arrMenu(0) = g_MenuItem1
                    arrMenu(1) = g_MenuItem2
                    arrMenu(2) = g_MenuItem3
                    arrMenu(3) = g_MenuItem4
                    arrMenu(4) = g_MenuItem5
                    arrMenu(5) = g_MenuItem6
                    arrMenu(6) = g_MenuItem7

                    If Admin.AddNewLayout(0, arrMenu, txtSelectedLayout.Text.Trim.ToString) = True Then
                        With msg
                            .Append("The Menu layout has been added new into the system by consultant : " & GlobalCRM.CurrentConName & " on " & CommonDataFunctions.GetLocalServer)
                            .Append(vbCrLf)
                            .Append("Menu Layout Name : " & lstConsView.SelectedItems(0).row.itemarray(1).ToString)
                            .Append(vbCrLf)
                            .Append("Column Names : " & txtColumn1.Text & ", " & txtColumn2.Text & ", ")
                            .Append(txtColumn3.Text & ", " & txtColumn4.Text & ", " & txtColumn5.Text)
                            .Append(", " & txtColumn6.Text & ", " & txtColumn7.Text & ", " & txtColumn8.Text & ", " & txtColumn9.Text)
                        End With
                        TasksEvents.GblFunc_Add_SysEvent("Admin (SL)", msg.ToString)
                    Else
                        MsgBox("There was an error creating the Menu Layout. Please try again", MsgBoxStyle.Information, "Menu Layout")
                    End If


                    btnSaveLayout.Content = "Save"
                    grpSelect.IsEnabled = False

                Case "Save"
                    'update function
                    msg = New StringBuilder
                    TasksEvents.GblFunc_Add_SysEvent("Admin (SL)", msg.ToString)
                    grpSelect.IsEnabled = False
            End Select
            btnSendLayout1.IsEnabled = False
            btnSendLayout2.IsEnabled = False
            btnSendLayout3.IsEnabled = False
            btnSendLayout4.IsEnabled = False
            btnSendLayout5.IsEnabled = False
            btnSendLayout6.IsEnabled = False
            btnSendLayout7.IsEnabled = False
            btnSendLayout8.IsEnabled = False
        Catch ex As Exception
        Finally
            TasksEvents = Nothing
            msg = Nothing
        End Try
    End Sub

    Private Sub rdoSelect_Checked(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles rdoSelect.Checked
        cboSelect.IsEnabled = True
        With Me.cboSelect
            .SelectedValuePath = "MenuOptionGroup_ID"
            .DisplayMemberPath = "MenuOptionGroup_Name"
            .ItemsSource = CType(Admin.MenuLayoutItemsCombo, IListSource).GetList
        End With
    End Sub

    Private Sub btnSendLayout1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnSendLayout1.Click
        If lstAvailColumns.SelectedItems.Count > 0 Then
            g_MenuItem1 = lstAvailColumns.SelectedItems(0).row.itemarray(0).ToString
            txtColumn2.Text = lstAvailColumns.SelectedItems(0).row.itemarray(1).ToString
        End If
    End Sub

    Private Sub btnSendLayout2_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnSendLayout2.Click
        If lstAvailColumns.SelectedItems.Count > 0 Then
            g_MenuItem2 = lstAvailColumns.SelectedItems(0).row.itemarray(0).ToString
            txtColumn3.Text = lstAvailColumns.SelectedItems(0).row.itemarray(1).ToString
        End If
    End Sub

    Private Sub btnSendLayout3_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnSendLayout3.Click
        If lstAvailColumns.SelectedItems.Count > 0 Then
            g_MenuItem3 = lstAvailColumns.SelectedItems(0).row.itemarray(0).ToString
            txtColumn4.Text = lstAvailColumns.SelectedItems(0).row.itemarray(1).ToString
        End If
    End Sub

    Private Sub btnSendLayout4_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnSendLayout4.Click
        If lstAvailColumns.SelectedItems.Count > 0 Then
            g_MenuItem4 = lstAvailColumns.SelectedItems(0).row.itemarray(0).ToString
            txtColumn5.Text = lstAvailColumns.SelectedItems(0).row.itemarray(1).ToString
        End If
    End Sub

    Private Sub btnSendLayout5_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnSendLayout5.Click
        g_MenuItem5 = lstAvailColumns.SelectedItems(0).row.itemarray(0).ToString
        txtColumn6.Text = lstAvailColumns.SelectedItems(0).row.itemarray(1).ToString
    End Sub

    Private Sub btnSendLayout6_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnSendLayout6.Click
        If lstAvailColumns.SelectedItems.Count > 0 Then
            g_MenuItem6 = lstAvailColumns.SelectedItems(0).row.itemarray(0).ToString
            txtColumn7.Text = lstAvailColumns.SelectedItems(0).row.itemarray(1).ToString
        End If
    End Sub

    Private Sub btnSendLayout7_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnSendLayout7.Click
        If lstAvailColumns.SelectedItems.Count > 0 Then
            g_MenuItem7 = lstAvailColumns.SelectedItems(0).row.itemarray(0).ToString
            txtColumn8.Text = lstAvailColumns.SelectedItems(0).row.itemarray(1).ToString
        End If
    End Sub

    Private Sub lstConsView_SelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles lstConsView.SelectionChanged
        Try
            If lstConsView.SelectedItems.Count > 0 Then
                btnEditLayout.IsEnabled = True
            End If
            txtSelectedLayout.Text = lstConsView.SelectedItems(0).row.itemarray(1).ToString()
            Dim dt As DataTable = Admin.MenuLayoutColumns(lstConsView.SelectedItems(0).row.itemarray(0).ToString())
            If Not dt Is Nothing Then
                If dt.Rows.Count > 0 Then
                    txtColumn2.Text = dt.Rows(0).Item("Menuoption_Name")
                End If
                If dt.Rows.Count > 1 Then
                    txtColumn3.Text = dt.Rows(1).Item("Menuoption_Name")
                End If
                If dt.Rows.Count > 2 Then
                    txtColumn4.Text = dt.Rows(2).Item("Menuoption_Name")
                End If
                If dt.Rows.Count > 3 Then
                    txtColumn5.Text = dt.Rows(3).Item("Menuoption_Name")
                End If
                If dt.Rows.Count > 4 Then
                    txtColumn6.Text = dt.Rows(4).Item("Menuoption_Name")
                End If
                If dt.Rows.Count > 5 Then
                    txtColumn7.Text = dt.Rows(5).Item("Menuoption_Name")
                End If
                If dt.Rows.Count > 6 Then
                    txtColumn8.Text = dt.Rows(6).Item("Menuoption_Name")
                End If
                If dt.Rows.Count > 7 Then
                    txtColumn9.Text = dt.Rows(7).Item("Menuoption_Name")
                End If
            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Sub btnEditLayout_IsEnabledChanged(ByVal sender As Object, ByVal e As System.Windows.DependencyPropertyChangedEventArgs) Handles btnEditLayout.IsEnabledChanged

        If btnEditLayout.IsEnabled = True Then
            'btnSaveLayout.IsEnabled = True
            'btnSaveLayout.Content = "Save"
        Else
            'btnSaveLayout.IsEnabled = False
        End If
    End Sub

    Private Sub btnNewLayout_IsEnabledChanged(ByVal sender As Object, ByVal e As System.Windows.DependencyPropertyChangedEventArgs) Handles btnNewLayout.IsEnabledChanged
        'If btnNewLayout.IsEnabled = True Then
        '    btnSaveLayout.IsEnabled = True
        '    btnSaveLayout.Content = "Save New"
        'Else
        '    btnSaveLayout.IsEnabled = False
        'End If
    End Sub

    Private Sub cboSelect_SelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles cboSelect.SelectionChanged
        LoadListColumnItems(cboSelect.SelectedValue)
    End Sub

    Private Sub rdoAll_Checked(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles rdoAll.Checked
        LoadListColumnItems(Admin.MenuOptionList.All)
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnCancel.Click
        btnEditLayout.IsEnabled = False
        btnSaveLayout.IsEnabled = False
        btnNewLayout.IsEnabled = True
    End Sub

    Private Sub btnSendLayout8_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnSendLayout8.Click
        If lstAvailColumns.SelectedItems.Count > 0 Then
            g_MenuItem8 = lstAvailColumns.SelectedItems(0).row.itemarray(0).ToString
            txtColumn9.Text = lstAvailColumns.SelectedItems(0).row.itemarray(1).ToString
        End If
    End Sub
End Class
